<template>
	<view class="content">
		<view class="field">
			<u-field v-model="phone" label="+86" placeholder="请填写手机号" :border-bottom="false">
			</u-field>
			<u-field v-model="code" label="验证码" placeholder="请填写验证码" :border-bottom="false">
				<view class="pwd" slot="right" @click="getCode">
					发送验证码
				</view>
			</u-field>
			<u-field class="linput" v-model="pwd" label="设置密码" placeholder="请设置登录密码" :border-bottom="false">
			</u-field>
		</view>
		<button class="new-btn" @click="submit">立即重置</button>
		<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				code: '',
				pwd: '',
				codeText: '发送验证码',
				yzPhone: '',
				yzCode: ''
			}
		},
		methods: {
			async getCode() {
				if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.phone)) return this.$u.toast('手机号码不正确')
				var code = Math.floor(Math.random() * (999999 - 100000 + 1) + 100000);
				if (this.$refs.uCode.canGetCode) {
					uni.showLoading({
						title: '正在获取验证码'
					})
					const {
						data: res
					} = await this.$Request({
						url: 'api/code/sms',
						data: {
							phone: this.phone,
							code: code
						}
					})
					if (res && res.code == 200) {
						this.yzPhone = this.phone;
						this.yzCode = code;
						uni.hideLoading();
						this.$u.toast('发送成功');
						this.$refs.uCode.start();
					}
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			codeChange(text) {
				this.codeText = text;
			},
			async submit() {
				if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.phone)) return this.$u.toast('手机号码不正确')
				if (!(this.pwd && this.pwd.length >= 6)) return this.$u.toast('密码不能为空并且不少于6位')
				if (!this.code || this.code != this.yzCode || this.phone != this.yzPhone) return this.$u.toast('验证码不正确')
				const {data:res} = await this.$Request({
					url: 'api/user/editPwd',
					data: {
						phone: this.phone,
						password: this.pwd
					}
				})
				if (res && res.code == 200) {
					this.$u.toast('修改密码成功');
					this.$store.commit('logout')
				} else {
					this.$u.toast(res.msg);
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		/* #ifndef H5 */
		min-height: 100vh;
		/* #endif */
		/* #ifdef H5 */
		min-height: calc(100vh - 44px);
		/* #endif */
		background-color: #2A2931;
		overflow: hidden;

		.field {
			margin: 60rpx 30rpx;

			.pwd {
				color: #A87B38;
			}

			/deep/.u-field {
				padding: 14px 20px;
				display: flex;
				align-items: center;
				position: relative;
				border-radius: 60px;
				margin-bottom: 30rpx;
				background: rgba(153, 149, 149, 0.466);

				.u-label-text {
					color: #A87B38;
				}

				input {
					color: #eee;
				}
			}
		}
	}
</style>
